{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Data readout basics i.e. for some analysis or plotting. \n",
    "This notebook is intended to show the **readout of created hdf5 files with python**. For the handling of the measured file qkit provides a **file info database (fid)** for convenient access to the stored data, for more info on that see the corresponding notebook.<br>\n",
    "The readout (as well as the storage in the first place) is done with the **`store.Data` module**. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## start qkit and import the needed modules. we here assume an already configured qkit analysis environment\n",
    "\n",
    "import qkit\n",
    "qkit.start()\n",
    "\n",
    "from qkit.storage.store import Data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The **`qkit.start()`** starts the fid which **searches the given `qkit.conf['datadir']` for hdf5 files**. A python dict is created, **mapping the 6-digit timestamp** of the measurement (UUID) **to its absolute path** on the hard drive. For getting the absolute path of the measurement file usethe **`qkit.fid.get` command with the UUID**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "abs_path = qkit.fid.get('XXXXXX')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To **open a measurement** and get the information a h5 object has to be created"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "h5 = Data(qkit.fid.get('XXXXXX'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The **data and metadata can be accessed via the h5 object**. All the needed information can be **auto-compleded via tabbing**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "amp = h5.data.amplitude[:] # gets the measurement data as numpy array\n",
    "pha = h5.data.phase[:]\n",
    "## general rule: h5.[folder].[ds_name] gives the dataset, '[:]' gives the bare data cast to a numpy array\n",
    "## already analyzed data is stored in the 'analysis' folder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# all stored metadata can be accessed by auto-complete as well\n",
    "x_ds_url = h5.data.amplitude.x_ds_url\n",
    "comment = h5.data.comment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The UUID can also be reverted back to recover the time the file was created."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qkit.fid.get_date('XXXXXX')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
